package com.qf.smartplatform.controller;

import com.qf.smartplatform.dto.Result;
import com.qf.smartplatform.pojo.Category;
import com.qf.smartplatform.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2022/1/4 10:12
 * description:
 */
@RestController
@RequestMapping("/categories")
public class CategoryController {

    private CategoryService categoryService;

    @Autowired
    public void setCategoryService(CategoryService categoryService){
        this.categoryService=categoryService;
    }

    @PostMapping("/addcategory")
    public Result addCategory(@RequestBody Category category){
        categoryService.addCategory(category);
        return Result.setOK();
    }

    @GetMapping("/categories")
    public Result getAllCategories(){
        List<Category> categoryList=categoryService.selectAllCategories();
        return Result.setOK(categoryList);
    }

    @PostMapping("/category/{id}")
    public Result getCategoryById(@PathVariable Long id){
        Category category=categoryService.selectById(id);
        return Result.setOK(category);
    }

    @GetMapping("/getbyname/")
    public Result getCategoryById(String cName){
        System.out.println("============");
        List<Category> categoryList=categoryService.selectCategoriesByNameLike(cName);
        return Result.setOK(categoryList);
    }

    @DeleteMapping("/categories")
    public Result deleteCategory(@RequestBody List<Long> ids){
        int result=categoryService.deleteCategoriesByIds(ids);
        return Result.setOK();
    }
}

